<?php

class getCharData {
	var $xmlInput;
	function readXml($file) {
		global $xmlCacheTime;
		global $xmlCacheDir;
		global $zone;
		global $char_realm;
		global $char_name;
		// cache and load xml file
		libxml_use_internal_errors(true);
		$xmlCacheFile = $xmlCacheDir.md5($zone.$char_realm.$char_name).".xml";
		if( file_exists($xmlCacheFile) && (time() - filemtime($xmlCacheFile)) < $xmlCacheTime) {
			$this->xmlInput = simplexml_load_file($xmlCacheFile)->characterInfo;
		}
		else {
			$this->xmlInput = simplexml_load_file($file)->characterInfo;
			if ($this->xmlInput) {
				copy($file,$xmlCacheFile);
			}
		}
	 return $this->xmlInput;
	}
	function data($type)  {
		switch($type) {
			default:
				return($this->xmlInput->character[$type]);
				break;
				//arena teams
			case '2on2_rating':
			case '3on3_rating':
			case '5on5_rating':
				$teamSize = (int)substr($type,0,1);
				for($i=0;$i<2;$i++) {
					if($this->xmlInput->character->arenaTeams->arenaTeam[$i]['size'] == $teamSize) {
	     return($this->xmlInput->character->arenaTeams->arenaTeam[$i]['rating']);
					}
				}
				break;
				//hp and mana
			case 'health':
				return($this->xmlInput->characterTab->characterBars->health['effective']);
				break;
			case 'mana':
				return($this->xmlInput->characterTab->characterBars->secondBar['effective']);
				break;
				//base stats
			case 'strength':
				return($this->xmlInput->characterTab->baseStats->strength['effective']);
				break;
			case 'agility':
				return($this->xmlInput->characterTab->baseStats->agility['effective']);
				break;
			case 'stamina':
				return($this->xmlInput->characterTab->baseStats->stamina['effective']);
				break;
			case 'intellect':
				return($this->xmlInput->characterTab->baseStats->intellect['effective']);
				break;
			case 'spirit':
				return($this->xmlInput->characterTab->baseStats->spirit['effective']);
				break;
				//melee stats
			case 'meleeAp':
				return($this->xmlInput->characterTab->melee->power['base']);
				break;
			case 'meleeHit':
				return($this->xmlInput->characterTab->melee->hitRating['increasedHitPercent']);
				break;
			case 'meleeArp':
				return($this->xmlInput->characterTab->melee->hitRating['reducedArmorPercent']);
				break;
			case 'meleeCrit':
				return($this->xmlInput->characterTab->melee->critChance['percent']);
				break;
			case 'expertise':
				return($this->xmlInput->characterTab->melee->expertise['value']);
				break;
				//ranged stats
			case 'rangedAp':
				return($this->xmlInput->characterTab->ranged->power['base']);
				break;
			case 'rangedHit':
				return($this->xmlInput->characterTab->ranged->hitRating['increasedHitPercent']);
				break;
			case 'rangedArp':
				return($this->xmlInput->characterTab->ranged->hitRating['reducedArmorPercent']);
				break;
			case 'rangedCrit':
				return($this->xmlInput->characterTab->ranged->critChance['percent']);
				break;
				//spell stats
				#always return highest spell power and crit
			case 'spellPower':
				return max(
	   $this->xmlInput->characterTab->spell->bonusDamage->arcane['value'],
	   $this->xmlInput->characterTab->spell->bonusDamage->fire['value'],
	   $this->xmlInput->characterTab->spell->bonusDamage->frost['value'],
	   $this->xmlInput->characterTab->spell->bonusDamage->holy['value'],
	   $this->xmlInput->characterTab->spell->bonusDamage->nature['value'],
	   $this->xmlInput->characterTab->spell->bonusDamage->shadow['value']
	   );
	   break;
			case 'healingPower':
				return($this->xmlInput->characterTab->spell->bonusHealing['value']);
				break;
			case 'spellHit':
				return($this->xmlInput->characterTab->spell->hitRating['increasedHitPercent']);
				break;
			case 'spellReduceResist':
				return($this->xmlInput->characterTab->spell->hitRating['reducedResist']);
				break;
			case 'spellCrit':
				return max(
	   $this->xmlInput->characterTab->spell->critChance->arcane['percent'],
	   $this->xmlInput->characterTab->spell->critChance->fire['percent'],
	   $this->xmlInput->characterTab->spell->critChance->frost['percent'],
	   $this->xmlInput->characterTab->spell->critChance->holy['percent'],
	   $this->xmlInput->characterTab->spell->critChance->nature['percent'],
	   $this->xmlInput->characterTab->spell->critChance->shadow['percent']
	   );
	   break;
			case 'haste':
				return($this->xmlInput->characterTab->spell->hasteRating['hastePercent']);
				break;
				//Defense
			case 'armor':
				return($this->xmlInput->characterTab->defenses->armor['base']);
				break;
			case 'defense':
				return($this->xmlInput->characterTab->defenses->defense['plusDefense']+$this->xmlInput->characterTab->defenses->defense['value']);
				break;
			case 'avoid':
				return($this->xmlInput->characterTab->defenses->defense['increasePercent']);
				break;
			case 'dodge':
				return($this->xmlInput->characterTab->defenses->dodge['increasePercent']);
				break;
			case 'parry':
				return($this->xmlInput->characterTab->defenses->parry['increasePercent']);
				break;
			case 'block':
				return($this->xmlInput->characterTab->defenses->block['increasePercent']);
				break;
			case 'resilience':
				return($this->xmlInput->characterTab->defenses->resilience['value']);
				break;

	 }
	}
	function talent($type) {
		if($this->xmlInput->characterTab->talentSpecs->talentSpec[0]['active']==1) {
			$specId = 0;
		}
		else {
			$specId = 1;
		}
		return($this->xmlInput->characterTab->talentSpecs->talentSpec[$specId][$type]);
	}
	function profession($id,$type) {
		return($this->xmlInput->characterTab->professions->skill[$id][$type]);
	}
	function getAvatarUrl() {
		global $avatarDir;
		global $avatarExtension;
		$url = $avatarDir;
		if($this->data('level') < 60) {
			$url .= "wow-default/";
		}
		elseif($this->data('level') < 70) {
			$url .= "wow/";
		}
		elseif($this->data('level') < 80) {
			$url .= "wow-70/";
		}
		else {
			$url .= "wow-80/";
		}
		$url = $url.$this->xmlInput->character['genderId']."-".$this->xmlInput->character['raceId']."-".$this->xmlInput->character['classId'];
		$url = $url.$avatarExtension;
		return $url;
	}
	function getClassImageUrl() {
		global $classImageDir;
		global $classImageExtension;
		$url = $classImageDir;
		$url = $url.$this->xmlInput->character['classId'];
		$url = $url.$classImageExtension;
		if(file_exists($url)) {
	  return $url;
	 }
	 else {
	  $url = ereg_replace  ($this->xmlInput->character['classId'],'none',$url);
	  return $url;
	 }
	}
	function getRaceImageUrl() {
		global $raceImageDir;
		global $raceImageExtension;
		$url = $raceImageDir;
		$url = $url.$this->xmlInput->character['raceId']."-".$this->xmlInput->character['genderId'];
		$url = $url.$raceImageExtension;
		if(file_exists($url)) {
	  return $url;
	 }
	 else {
	  $url = ereg_replace($this->xmlInput->character['raceId']."-".$this->xmlInput->character['genderId'],'none',$url);
	  return $url;
	 }
	}
	function getProfessionImageUrl($id) {
		global $professionImageDir;
		global $professionImageExtension;
		$url = $professionImageDir;
		$url = $url.$this->xmlInput->characterTab->professions->skill[$id]['key'];
		$url = $url."-sm".$professionImageExtension;
		if(file_exists($url)) {
	  return $url;
	 }
	 else {
	  $url = ereg_replace($this->xmlInput->characterTab->professions->skill[$id]['key']."-sm",'none',$url);
	  return $url;
	 }
	}
	function getTalentImageUrl() {
		global $talentImageDir;
		global $talentImageExtension;
		$talent = $this->talent('icon');
		$url = $talentImageDir.$talent.$talentImageExtension;
		if(file_exists($url)) {
	  return $url;
	 }
	 else {
	  $url = $talentImageDir.'none'.$talentImageExtension;
	  return $url;
	 }
	}
}

?>